#!/usr/bin/env python
#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.

import bencode
import log_parser

import collections
import cPickle
import sys
import vuze_util

def main():
  TARGET_KEYS = dict(eval(open(sys.argv[1]).read()))
  KEY_IDS = {}
  nkeys = 0
  for key in TARGET_KEYS:
    KEY_IDS[key] = nkeys
    nkeys += 1
  pickle_file_name = '%s.pickle' % sys.argv[2]
  print 'Pickle file: %s' % pickle_file_name
  found_keys = collections.defaultdict(lambda: sys.maxint)
  sybil_finds = []
  total_stores = 0
  total_bytes = 0
  bencoded_stores = 0
  bencoded_bytes = 0
  peer_record_stores = 0
  peer_record_bytes = 0
  unvanish_acceptable_stores = 0
  unvanish_acceptable_bytes = 0
#  unique_keys = set()
  value_bytes = 0
  for log in sys.argv[3:]:
    log_list = log_parser.get_stores(log)
    if not log_list:
      sybil_finds.append([])
      continue
    this_sybil_finds = []
    total_stores += len(log_list)
    t0 = log_list[0][1]
    for (key, store_time, version, value) in log_list:
      total_bytes += len(value)
      if ((len(value) <= 5 and value.isdigit()) or
          vuze_util.is_peer_record(value)):
        peer_record_stores += 1
        peer_record_bytes += len(value)
        continue

      if bencode.simple_is_bencoded(value):
        bencoded_stores += 1
        bencoded_bytes += len(value)
        continue

      if 16 <= len(value) <= 51:
        unvanish_acceptable_stores += 1
        unvanish_acceptable_bytes += len(value)

#      unique_keys.add(key)
      value_bytes += len(value)
      if key in TARGET_KEYS:
        if value == TARGET_KEYS[key]:
          this_sybil_finds.append(KEY_IDS[key])
          found_keys[key] = min(store_time-t0, found_keys[key])
#          print 'Key %s FOUND in %s, stored at %s' % (key.encode('hex'), log, store_time)
#        else:
#          print 'Key %s found with wrong value %s instead of %s' %(key.encode('hex'), TARGET_KEYS[key].encode('hex'), value.encode('hex'))

    sybil_finds.append(this_sybil_finds)

  cPickle.dump({'found_keys': dict(found_keys.iteritems()),
                'total_stores': total_stores, # 'unique_keys': unique_keys,
                'total_bytes': total_bytes,
                'bencoded_stores': bencoded_stores,
                'bencoded_bytes': bencoded_bytes,
                'peer_record_stores': peer_record_stores,
                'peer_record_bytes': peer_record_bytes,
                'unvanish_acceptable_stores': unvanish_acceptable_stores,
                'unvanish_acceptable_bytes': unvanish_acceptable_bytes,
                'value_bytes': value_bytes,
                'sybil_finds': sybil_finds},
               open(pickle_file_name, 'wb'), protocol=cPickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
  main()
